package com.lemon.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.struts2.dispatcher.HttpParameters;
import org.apache.struts2.dispatcher.Parameter;

import com.lemon.domain.Department;
import com.lemon.service.DepartmentService;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

public class DepartmentAction extends ActionSupport implements ModelDriven<Department> {

	private static final long serialVersionUID = 1L;
	private DepartmentService departmentService = new DepartmentService();

	private Department department = new Department();

	//json数据集合
	private Map<String, Object> json;

	public String resultJSON() {
		ActionContext context = ActionContext.getContext();
		// 获取部门ID
		String param = getParam(context, "id");
		Long id = Long.valueOf(param);
		// 根据ID查询部门
		Department department = departmentService.get(id);
		json = new HashMap<>();
		json.put("id", department.getId());
		json.put("name", department.getName());
		if (department.getSuperiorDepartment() != null) {
			json.put("superiorDepartmentName", department.getSuperiorDepartment().getName());
		}
		json.put("description", department.getDescription());

		return SUCCESS;
	}

	/**
	 * 添加一个部门
	 * 
	 * @return
	 */
	public String save() {
		departmentService.save(department);
		return SUCCESS;
	}

	/**
	 * 修改部门信息
	 * 
	 * @return
	 */
	public String edit() {
		departmentService.update(department);
		return SUCCESS;
	}

	/**
	 * 删除部门
	 * 
	 * @return
	 */
	public String delete() {
		ActionContext context = ActionContext.getContext();
		// 被删除部门的ID
		String param = getParam(context, "id");
		Long id = Long.valueOf(param);
		departmentService.delete(id);

		return SUCCESS;
	}

	/**
	 * 跳转到修改页面
	 * 
	 * @return
	 */
	public String toEdit() {
		ActionContext context = ActionContext.getContext();
		// 获取要修改的部门的ID
		String param = getParam(context, "id");
		Long id = Long.valueOf(param);
		// 根据ID查询部门
		Department department = departmentService.get(id);
		context.put("department", department);

		return SUCCESS;
	}

	/**
	 * 跳转到列表页面
	 * 
	 * @return
	 */
	public String toList() {
		ActionContext context = ActionContext.getContext();
		String departmentListHtml = departmentService.listToHtml();
		context.put("departmentListHtml", departmentListHtml);

		return SUCCESS;
	}

	/**
	 * 跳转到保存页面
	 * 
	 * @return
	 */
	public String toSave() {
		ActionContext context = ActionContext.getContext();
		List<Department> departments = departmentService.list();
		context.put("departments", departments);
		return SUCCESS;
	}

	/**
	 * 封装了参数获取
	 * 
	 * @param context
	 * @param key
	 * @return
	 */
	private String getParam(ActionContext context, String key) {
		HttpParameters parameters = context.getParameters();
		Parameter param = parameters.get(key);
		return param.getValue();
	}

	@Override
	public Department getModel() {
		return department;
	}

	public Map<String, Object> getJson() {
		return json;
	}

	public void setJson(Map<String, Object> json) {
		this.json = json;
	}

}
